package com.example.kotlinlearndemo

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.PopupWindow
import android.widget.Toast
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.lifecycleScope
import com.example.kotlinlearndemo.databinding.ActivityMainBinding
import com.example.kotlinlearndemo.expand.ppp
import com.example.kotlinlearndemo.expand.showToast
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine

class MainActivity : AppCompatActivity() {

    // Use the 'by viewModels()' Kotlin property delegate
    // from the activity-ktx artifact
    //第一种创建
    val viewModel: MyViewModel by viewModels()

    var viewBinding: ActivityMainBinding? = null

    companion object {
        var TAG = "MainActivity"
    }

    val f: FooModel by lazy {
        FooModel().apply {
            a = TAG
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        TAG = "123"

        println("12345: " + f.a)

        //第二种创建
        //viewModel=defaultViewModelProviderFactory.create(MyViewModel::class.java)
        //第三种创建
        //viewModel=MyViewModel()

        viewBinding =
            DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main);

        viewBinding?.lifecycleOwner = this

        viewBinding?.viewmodel = viewModel

        viewBinding?.handlers = MyHandlers()

        viewBinding?.viewmodel?.observe(this)

        viewBinding?.expandViewClick?.showToast(
            "ss"
        )
        viewBinding?.expandViewClick?.ppp

        lifecycleScope.launch {
            delay(2000)
        }

        lifecycleScope.launch {
            Log.d(TAG, "12345: " + test())
            Log.d(TAG, "12345: end")
        }
    }

    suspend fun test(): Boolean {
        return suspendCoroutine {
            lifecycleScope.launch {
                delay(3000)
                Log.d(TAG, "test: delay结束")
                it.resume(false)
            }
        }
    }
}

class P : PopupWindow() {

    companion object {
        val TAG = "P"

        fun show(view: View) {
            Log.d(TAG, "show: ")
            val window = P()

            val content =
                LayoutInflater.from(view.context).inflate(R.layout.test_p, null, false)

            content.measure(
                View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED
            )

            Log.d(TAG, "size1: " + content.measuredWidth + " " + content.measuredHeight)
            Log.d(TAG, "size1: " + content.width + " " + content.height)

            content.setOnClickListener {
                Toast.makeText(view.context, "消失了啊", Toast.LENGTH_SHORT).show()
                window.dismiss()
            }

            window.apply {
                width = content.measuredWidth
//            width = ViewGroup.LayoutParams.WRAP_CONTENT
                height = content.measuredHeight
                contentView = content
                isFocusable = false
                isOutsideTouchable = true

                showAsDropDown(view,
                    (view.width - window.width) / 2,
                    -view.height - window.height)
            }
        }
    }

}